
rng('shuffle')

%%% generation and assignment of random types
type_distrib=randn(ind_n,1);

type_th=NaN(n_type-1,1);
for z=1:(n_type-1)
    type_th(z)=norminv(sum(est_p_types(1:z)));
end;

type=ones(size(type_distrib,1),size(type_distrib,2));
p_types=zeros(n_type,1);
for z=1:(n_type-1)
    type(type_distrib>type_th(z))=z+1;
end;

for z=1:(n_type)
    p_types(z)=sum(type==z)/ind_n;
end;

thetas=[NaN; est_thetas];
kappas=[NaN; est_kappas];
sig_ths=[NaN; est_sig_ths];


%%% Each column is a type, thus composed of 5 intercepts, one for each of
%%% the estimated preference and conistency parameters
types=est_types;

%%% kappa - the tremble parameter which represents the fraction of the time
%%% the individual makes the "wrong" choice 
kappa_types=NaN(ind_n,1);
kappa=NaN(ind_n,1);
for z=1:n_type
    temp=kron(ones(ind_n*per_n,1), types(2,z))+kappas(2)*X_s(:,1)+factors*kappas(3:end);
    kappa_types=0.5*normcdf(temp);
    %%% combined kappa by type
    kappa(type==z)=kappa_types(type==z);
end;


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Risk-Aversion %%%%%%%%%%%%%%%%%%%%%%

%%% sig_th is the standard deviation of an individual's error term on the
%%% coefficient of risk-aversion
sig_th_types=NaN(ind_n,1);
sig_th=NaN(ind_n,1);
for z=1:n_type
    temp=kron(ones(ind_n*per_n,1), types(3,z))+sig_ths(2)*X_s(:,1)+factors*sig_ths(3:end);
    sig_th_types=sig_th_scale*normcdf(temp);
    %%% combined sig_th by type
    sig_th(type==z)=sig_th_types(type==z);
end;


%%% True (or average) part of risk-aversion
theta_i_types=NaN(ind_n,1);
theta_i=NaN(ind_n,1);
for z=1:n_type
    temp=kron(ones(ind_n*per_n,1), types(1,z))+thetas(2)*X_s(:,1)+factors*thetas(3:end);
    theta_i_types=temp;
    %%% combined theta by type
    theta_i(type==z)=theta_i_types(type==z);
end;

